package com.szty.libarytest.mediaProjection

import android.content.Context
import android.graphics.*
import android.graphics.Bitmap.Config.ARGB_8888
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import com.szty.libarytest.R
import com.szty.libarytest.other.loge

/**
 *@author : created by SunJH
 *时间：2021/9/17 001715
 *作用：
 */
class onlyDrawView(context: Context, attributeSet: AttributeSet) : View(context, attributeSet) {
    private val mPaint = Paint()
    private val path = Path()
    private var bufferBitmap: Bitmap? = null
    private var bufferCanvas: Canvas? = null
    private var lastY = 0f
    private var lastX = 0f

    init {
        mPaint.setColor(Color.BLACK)
        mPaint.style = Paint.Style.STROKE
        mPaint.isAntiAlias = true
        mPaint.isDither = true
        mPaint.strokeWidth = 2f
        setBackgroundResource(R.drawable.sp_bg_p0)
    }

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        bufferBitmap = Bitmap.createBitmap(w, h, ARGB_8888)
        bufferCanvas = Canvas(bufferBitmap!!)
    }

    override fun onTouchEvent(event: MotionEvent): Boolean {
        when (event.action) {
            MotionEvent.ACTION_DOWN -> {
                path.moveTo(event.x, event.y)
                lastX = event.x
                lastY = event.y
            }
            MotionEvent.ACTION_MOVE -> {
                path.quadTo(lastX, lastY, (event.x + lastX) / 2, (event.y + lastY) / 2)
                lastY = event.y
                lastX = event.x
            }
            MotionEvent.ACTION_UP -> {
                bufferCanvas?.drawPath(path,mPaint)
                path.reset()
            }
        }
        invalidate()
        return true
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        canvas?.drawBitmap(bufferBitmap!!,0f,0f,mPaint)
//        canvas?.drawPath(path,mPaint)
    }

}